package service

import (
	"context"
	"review-service/api/review/v1"
	"review-service/dal/model"
	"review-service/internal/biz"
)

type ReviewService struct {
	v1.UnimplementedReviewServer
	uc *biz.ReviewUsecase
}

func NewReviewService(uc *biz.ReviewUsecase) *ReviewService {
	return &ReviewService{
		uc: uc,
	}
}

func (s *ReviewService) CreateReview(ctx context.Context, req *v1.CreateReviewRequest) (*v1.CreateReviewReply, error) {
	a := 0
	if req.Anonymous {
		a = 1
	}
	review, err := s.uc.CreateReview(ctx, &model.ReviewInfo{
		UserID:       req.UserID,
		OrderID:      req.OrderID,
		StoreID:      req.StoreID,
		Score:        req.Score,
		ServiceScore: req.ServiceScore,
		ExpressScore: req.ExpressScore,
		Content:      req.Content,
		PicInfo:      req.PicInfo,
		VideoInfo:    req.VideoInfo,
		Anonymous:    int32(a),
		Status:       10,
	})
	if err != nil {
		return nil, err
	}
	//拼装返回结果
	return &v1.CreateReviewReply{
		ReviewID: review.ReviewID,
	}, nil
}

func (s *ReviewService) GetReview(ctx context.Context, req *v1.GetReviewRequest) (*v1.GetReviewReply, error) {
	review, err := s.uc.GetReview(ctx, req.ReviewID)
	if err != nil {
		return nil, err
	}
	data := &v1.ReviewInfo{
		ReviewID:     review.ReviewID,
		UserID:       review.UserID,
		OrderID:      review.OrderID,
		Score:        review.Score,
		ServiceScore: review.ServiceScore,
		ExpressScore: review.ExpressScore,
		Content:      review.Content,
		PicInfo:      review.PicInfo,
		VideoInfo:    review.VideoInfo,
		Status:       review.Status,
	}
	return &v1.GetReviewReply{
		Data: data,
	}, nil
}

func (s *ReviewService) AuditReview(ctx context.Context, req *v1.AuditReviewRequest) (*v1.AuditReviewReply, error) {
	var opRemarks string
	if req.OpRemarks == nil {
		opRemarks = ""
	} else {
		opRemarks = *req.OpRemarks
	}
	r := &model.ReviewInfo{
		ReviewID:  req.ReviewID,
		Status:    req.Status,
		OpUser:    req.OpUser,
		OpReason:  req.OpReason,
		OpRemarks: opRemarks,
	}
	res, err := s.uc.AuditReview(ctx, r)
	if err != nil {
		return nil, err
	}
	return &v1.AuditReviewReply{
		ReviewID: res.ReviewID,
		Status:   res.Status,
	}, nil
}

func (s *ReviewService) ReplyReview(ctx context.Context, req *v1.ReplyReviewRequest) (*v1.ReplyReviewReply, error) {
	reply, err := s.uc.CreateReply(ctx, &biz.ReplyParam{
		ReviewID:  req.ReviewID,
		StoreID:   req.StoreID,
		Content:   req.Content,
		PicInfo:   req.PicInfo,
		VideoInfo: req.VideoInfo,
	})
	if err != nil {
		return nil, err
	}

	return &v1.ReplyReviewReply{
		ReplyID: reply.ReviewID,
	}, nil
}

func (s *ReviewService) AppealReview(ctx context.Context, req *v1.AppealReviewRequest) (*v1.AppealReviewReply, error) {
	r := &model.ReviewAppealInfo{
		ReviewID:  req.ReviewID,
		StoreID:   req.StoreID,
		Reason:    req.Reason,
		Content:   req.Content,
		PicInfo:   req.PicInfo,
		VideoInfo: req.VideoInfo,
	}
	ret, err := s.uc.AppealReview(ctx, r)
	if err != nil {
		return nil, err
	}
	return &v1.AppealReviewReply{
		AppealID: ret.AppealID,
	}, nil
}

func (s *ReviewService) AuditAppeal(ctx context.Context, req *v1.AuditAppealRequest) (*v1.AuditAppealReply, error) {
	opremark := ""
	if req.OpRemarks != nil {
		opremark = *req.OpRemarks
	}
	r := &model.ReviewAppealInfo{
		AppealID:  req.AppealID,
		ReviewID:  req.ReviewID,
		Status:    req.Status,
		OpUser:    req.OpUser,
		OpRemarks: opremark,
	}
	err := s.uc.AuditAppeal(ctx, r)
	return &v1.AuditAppealReply{}, err
}

func (s *ReviewService) ListReviewByStoreID(ctx context.Context, req *v1.ListReviewByStoreIDRequest) (*v1.ListReviewByStoreIDReply, error) {
	ret, err := s.uc.ListReviewByStoreID(ctx, req.StoreID, req.Page, req.Size)
	if err != nil {
		return nil, err
	}
	list := make([]*v1.ReviewInfo, 0, len(ret))
	for _, v := range ret {
		list = append(list, &v1.ReviewInfo{
			ReviewID:     v.ReviewID,
			UserID:       v.UserID,
			OrderID:      v.OrderID,
			Score:        v.Score,
			ServiceScore: v.ServiceScore,
			ExpressScore: v.ExpressScore,
			Content:      v.Content,
			PicInfo:      v.PicInfo,
			VideoInfo:    v.VideoInfo,
			Status:       v.Status,
		})
	}
	return &v1.ListReviewByStoreIDReply{
		List: list,
	}, nil
}
